package com.jblank.action.business.system;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.jblank.core.action.BizAction;
import com.jblank.dto.DTO;
import com.jblank.dto.impl.BaseDTO;


/**
 * 用户管理Action
 * @author kim
 * @since 2013-07-23
 */
@Component("userAction")
@Scope("prototype")
public class UserAction extends BizAction {
	private static Log log = LogFactory.getLog(UserAction.class);
	private static final long serialVersionUID = 1L;
	
	/**
	 * 登录初始化
	 * @return
	 */
	public String loginInit() {
		System.out.println(this);
		return "loginInit";
	}
	
	/**
	 * 登录
	 * @return
	 */
	public String login() throws Exception{
		String tableName = "sys_user";
		String username = dto.getAsString("username");
		String password = dto.getAsString("password");
		//密码加密
		log.info("[" + username + "]" + "尝试登录系统！");
		String condExp = "scode = ? and spassword = ?";
		String userId = (String)dao.queryForObject(tableName, "sid", condExp, new String[]{username,password});
		DTO jsonDto = new BaseDTO();
		if(userId != null && userId.length() > 0){
			jsonDto.put("success", new Boolean(true));
			jsonDto.put("userid", userId);
			write(jsonDto.toJson(), response);
		}else{
			jsonDto.put("success", new Boolean(false));
			jsonDto.setMsg("用户不存在或者密码不正确！");
			write(jsonDto.toJson(), response);
		}
		return null;
	}
}
